%{
#include <ctype.h>
#include <stdio.h>
#include "common.h"

unsigned int lineNumber;

/* The value that yylex returns must be the positive numeric code 
for the type of token it has just found; a zero or negative value 
signifies end-of-input. */
%}

%option noyywrap

white_space      [ \t]*
digit            [0-9]
operator         [+-/*]
number           (-?{digit}+(\.{digit}+)?)|(\.{digit}+)

%%

{number}         { printf("FLEX : NUMBER\n"); sscanf(yytext, "%lf", &yylval); return NUMBER; }
{operator}       { printf("FLEX : OPERATOR\n"); return *yytext; }
\(               { printf("FLEX : '('\n"); return *yytext; }
\)               { printf("FLEX : ')'\n"); return *yytext; }

<<EOF>>          { printf("FLEX : EOF\n"); return 0; }
"EXIT"           { printf("FLEX : EXIT\n"); return 0; }
\n               { printf("FLEX : \\n\n"); ++lineNumber; return '\n'; }
{white_space}    { printf("FLEX : ws\n"); /* do nothing */ }
.                { yyerror("Illegal input"); }

%%fd

void yyerror(const char *msg)
{
	if (*yytext == 0)
		fprintf(stderr, "Error on line %d: \"%s\" (Null character)\n", lineNumber, msg);
	else
		fprintf(stderr, "Error on line %d: \"%s\"\n", lineNumber, msg);
}